<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\controller\Base;
use app\common\model\Withdraw as WithdrawModel;
use app\common\model\User;
use think\facade\Db;
use \think\Request;

class Withdraw extends Base
{

    /**
     * 申请体现
     */
    public function index(Request $request)
    {
        $type = $request->param("type");
        $money = $request->param("money");
        $name = $request->param("name");
        $number = $request->param("number");
        $bank = $request->param("bank");

        if (empty($type)) {
            return $this->renderError("请选择提现方式");
        }
        if (!in_array($type, array(2, 3))) {
            return $this->renderError("提现方式错误");
        }
        if ($type == 2) {
            if (empty($name)) {
                return $this->renderError("请输入支付宝姓名");
            }
            if (empty($number)) {
                return $this->renderError("请输入支付宝账号");
            }
        } elseif ($type == 3) {
            if (empty($bank)) {
                return $this->renderError("请输入银行卡开户行");
            }
            if (empty($name)) {
                return $this->renderError("请输入银行卡姓名");
            }
            if (empty($number)) {
                return $this->renderError("请输入银行卡账号");
            }
        }
        //获取用户信息
        $user = $this->getUser();
        $money = floatval($money);
        if ($money <= 0) {
            return $this->renderError("提现金额错误");
        }
        if ($money > $user['money2']) {
            return $this->renderError("提现金额不能超过集市余额");
        }
        $config = getConfig('base');
        if ($money < $config['withdraw_money']) {
            return $this->renderError("提现金额" . $config['withdraw_money'] . "起提");
        }

        if (bcadd("$money",$config['withdraw_money_sxf'],2) > $user['money2']) {
            return $this->renderError('集市余额不足以支付手续费');
        }
        $start_time = strtotime(date('Y-m-d',time()).' 00:00:00');
        
        // $sum_money = Db::name('withdraw')->where([['user_id', '=', $user['id'],['addtime','>=',$start_time]]])->whereIn('status',[1,2])->sum('money');
        $sum_money = Db::name('withdraw')
            ->where('user_id', $user['id'])
            ->where('addtime','>=',$start_time)
            ->where('status','in',[1,2])
            ->sum('money');
        if (round($sum_money + $money,2)  > 5000){
            return $this->renderError('当天提现金额不能大于5000');
        }
        Db::startTrans();
        try {
            $sxf = $config['withdraw_money_sxf'];
            $arr['user_id'] = $user['id'];
            $arr['type'] = $type;
            $arr['imgurl'] = '';
            $arr['status'] = 1;
            $arr['addtime'] = time();
            $arr['tal_money'] = $money;
            $arr['money'] = $money;
            $arr['sxf'] = $sxf;
            $arr['name'] = $name;
            $arr['number'] = $number;
            $arr['bank'] = $bank;
            $dd = WithdrawModel::insert($arr);
            if ($dd) {
                $res = User::changeMoney2($user['id'], -$money, 5, '提现');
                if ($sxf > 0){
                    $res2 = User::changeMoney2($user['id'], -$sxf, 7, '提现手续费');
                    if (!$res2) {
                        Db::rollback();
                        return $this->renderError('提现申请失败!');
                    }
                }

                if (!$res) {
                    Db::rollback();
                    return $this->renderError('提现申请失败!!');
                }
                Db::commit();
                return $this->renderSuccess('提现申请成功，请等待后台审核');
            } else {
                Db::rollback();
                return $this->renderError('提现申请失败!!!');
            }
        }catch (\Exception $e){
            Db::rollback();
            return $this->renderError('提现申请失败!!!!');
        }
    }

    /**
     * 提现记录
     */
    public function record(Request $request)
    {
        $user = $this->getUser();
        $whe[] = ['user_id', '=', $user['id']];
        $list = WithdrawModel::getList($whe, 'id,tal_money,money,status,reason,addtime','id desc');
        foreach ($list['list'] as $k => &$v) {
            $v['addtime'] = date("Y-m-d H:i", $v['addtime']);
            if ($v['status'] == 1) {
                $v['status_text'] = '未审核';
            } elseif ($v['status'] == 2) {
                $v['status_text'] = '已通过';
            } elseif ($v['status'] == 3) {
                $v['status_text'] = '未通过';
            } else {
                $v['status_text'] = '';
            }
        }
        //提现手续费
        $config = getConfig('base');
        $list['max_money'] = 5000;
        $list['withdraw_money'] = $config['withdraw_money'];
        $list['withdraw_money_sxf'] = $config['withdraw_money_sxf'];


        //上次提现的卡号
        $card_data = Db::name('withdraw')->field('type,name,number,bank')->where([['user_id','=',$user['id']],['type','=',3]])->order('id desc')->find();
        $card_data2 = Db::name('withdraw')->field('type,name,number,bank')->where([['user_id','=',$user['id']],['type','=',2]])->order('id desc')->find();
        $list['yinhangka'] = $card_data;
        $list['zhifubao'] = $card_data2;

        return $this->renderSuccess('请求成功', $list);
    }

}